const area = {
    prepareData(data) {
        var initNum = 100;
        var stepNum = 1;
        for (var i in data) {
            var provinceNum = initNum++;
            var province = data[i];
            province.id = provinceNum;
            province.name = province.province;
            province.parentId = 0;
            province.parentName = "";
            delete province.province;
            var cityInitNum = parseInt(provinceNum + '' + stepNum);
            for (var j in province.citys) {
                var cityNum = cityInitNum++;
                var city = province.citys[j];
                city.id = cityNum;
                city.name = city.city;
                city.parentId = province.id;
                city.parentName = province.name;
                delete city.city;
                var areaInitNum = parseInt(cityNum + '' + stepNum);
                for (var k in city.areas) {
                    var areaNum = areaInitNum++;
                    var area = city.areas[k];
                    area.id = areaNum;
                    area.name = area.area;
                    area.parentId = city.id;
                    area.parentName = city.name;
                    delete area.area;
                }
            }
        }
        console.log(data)
    },
    initData(data) {
        const provinceList = this.provinceList(data);
        const cityList = this.cityList(data, provinceList[0].id);
        const countryList = this.countryList(data, provinceList[0].id, cityList[0].id);
        return {
            province: provinceList,
            city: cityList,
            country: countryList
        }
    },
    provinceList(data) {
        var list = [];
        data.forEach(item => {
            list.push({ id: item.id, name: item.name, parentId: item.parentId })
        })
        return list;
    },
    cityList(data, provinceId) {
        for (var i in data) {
            if (data[i].id == provinceId) {
                var list = [];
                data[i].citys.forEach(item => {
                    list.push({ id: item.id, name: item.name, parentId: item.parentId })
                })
                return list;
            }
        }
    },
    countryList(data, provinceId, cityId) {
        for (var i in data) {
            if (data[i].id == provinceId) {
                var citys = data[i].citys;
                for (var j in citys) {
                    if (citys[j].id == cityId) {
                        var list = [];
                        citys[j].areas.forEach(item => {
                            list.push({ id: item.id, name: item.name, parentId: item.parentId })
                        })
                        return list;
                    }
                }
            }
        }
    },
}

export default area;